
<!DOCTYPE html>
<html lang="en" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>SpringMVC - Hexo</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="Fechin,"> 
    <meta name="description" content="Spring MVC1. SpringMVC快速入门

导入SpringMVC相关坐标


12345&amp;lt;dependency&amp;gt;  &amp;lt;groupId&amp;gt;org.springfra,"> 
    <meta name="author" content="John Doe"> 
    <link rel="alternative" href="atom.xml" title="Hexo" type="application/atom+xml"> 
    <link rel="icon" href="/yi-mark-gitee-io/img/favicon.png"> 
    
    
    
    <meta name="twitter:card" content="summary"/>
    <meta name="twitter:title" content="SpringMVC - Hexo"/>
    <meta name="twitter:description" content="Spring MVC1. SpringMVC快速入门

导入SpringMVC相关坐标


12345&amp;lt;dependency&amp;gt;  &amp;lt;groupId&amp;gt;org.springfra,"/>
    
    
    
    
    <meta property="og:site_name" content="Hexo"/>
    <meta property="og:type" content="object"/>
    <meta property="og:title" content="SpringMVC - Hexo"/>
    <meta property="og:description" content="Spring MVC1. SpringMVC快速入门

导入SpringMVC相关坐标


12345&amp;lt;dependency&amp;gt;  &amp;lt;groupId&amp;gt;org.springfra,"/>
    
<link rel="stylesheet" href="/yi-mark-gitee-io/css/diaspora.css">

<meta name="generator" content="Hexo 6.0.0"></head>

<body class="loading">
    <span id="config-title" style="display:none">Hexo</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="iconfont icon-home image-icon" href="javascript:;" data-url="https://yi-mark.gitee.io"></a>
    <div title="播放/暂停" class="iconfont icon-play"></div>
    <h3 class="subtitle">SpringMVC</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="iconfont icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">SpringMVC</h1>
        <div class="stuff">
            <span>二月 01, 2022</span>
            
  <ul class="post-tags-list" itemprop="keywords"><li class="post-tags-list-item"><a class="post-tags-list-link" href="/yi-mark-gitee-io/tags/SpringMVC/" rel="tag">SpringMVC</a></li></ul>


        </div>
        <div class="content markdown">
            <h1 id="Spring-MVC"><a href="#Spring-MVC" class="headerlink" title="Spring MVC"></a>Spring MVC</h1><h2 id="1-SpringMVC快速入门"><a href="#1-SpringMVC快速入门" class="headerlink" title="1. SpringMVC快速入门"></a>1. SpringMVC快速入门</h2><blockquote>
<ol>
<li>导入SpringMVC相关坐标</li>
</ol>
</blockquote>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-webmvc<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">version</span>&gt;</span>5.0.5.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<blockquote>
<ol start="2">
<li>配置SpringMVC核心控制器DispatcherServlet</li>
</ol>
</blockquote>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--  在web.xml中配置--&gt;</span></span><br><span class="line"><span class="comment">&lt;!--  配置SpringMVC的前端控制器--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">servlet</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>DispatcherServlet<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">servlet-class</span>&gt;</span>org.springframework.web.servlet.DispatcherServlet<span class="tag">&lt;/<span class="name">servlet-class</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">init-param</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">param-name</span>&gt;</span>contextConfigLocation<span class="tag">&lt;/<span class="name">param-name</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">param-value</span>&gt;</span>classpath:spring-mvc.xml<span class="tag">&lt;/<span class="name">param-value</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">init-param</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">load-on-startup</span>&gt;</span>1<span class="tag">&lt;/<span class="name">load-on-startup</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">servlet</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="tag">&lt;<span class="name">servlet-mapping</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>DispatcherServlet<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">url-pattern</span>&gt;</span>/<span class="tag">&lt;/<span class="name">url-pattern</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">servlet-mapping</span>&gt;</span></span><br></pre></td></tr></table></figure>







<blockquote>
<ol start="3">
<li>创建Controller类和视图页面</li>
</ol>
</blockquote>
<blockquote>
<ol start="4">
<li>使用注解配置Controller类中的业务方法的映射地址</li>
</ol>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserController</span> </span>&#123;</span><br><span class="line">    <span class="meta">@RequestMapping(&quot;/user&quot;)</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">index</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Controller is running....&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;hello.html&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<blockquote>
<ol start="5">
<li>配置SpringMVC核心配置文件spring-mvc.xml</li>
</ol>
</blockquote>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">&quot;http://www.springframework.org/schema/beans&quot;</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:xsi</span>=<span class="string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:context</span>=<span class="string">&quot;http://www.springframework.org/schema/context&quot;</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xsi:schemaLocation</span>=<span class="string">&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd</span></span></span><br><span class="line"><span class="string"><span class="tag">                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd&quot;</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--Controller组件扫描, 用来扫描Controller注解--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">context:component-scan</span> <span class="attr">base-package</span>=<span class="string">&quot;com.yihen.controller&quot;</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--mvc注解驱动,用来扫描@RequestMapping--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">mvc:annotation-driven</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--静态资源权限开放--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">mvc:default-servlet-handler</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>&#x3D;&#x3D;注意: 不开放静态资源权限和mvc注解驱动访问不到页面&#x3D;&#x3D;</p>
</blockquote>
<h2 id="2-SpringMVC注解解析"><a href="#2-SpringMVC注解解析" class="headerlink" title="2. SpringMVC注解解析"></a>2. SpringMVC注解解析</h2><blockquote>
<ol>
<li>@RequestMapping:用于建立请求URL和处理请求方法之间的对应关系</li>
</ol>
</blockquote>
<ul>
<li>位置:<ul>
<li>类上：请求URL的第一级访问目录。此处不写的话，相当于应用的根目录</li>
<li>方法上：请求URL的第二级访问目录，与类上的使用@RequestMapping 标注的一级目录一起组成访问虚拟路径</li>
</ul>
</li>
<li>属性:<ul>
<li>value：用于指定请求的URL。它和path属性的作用是一样的</li>
<li>method：用于指定请求的方式</li>
<li>params：用于指定限制请求参数的条件。它支持简单的表达式。要求请求的参数的key和value必须和配置的一模一样</li>
</ul>
</li>
<li>例如:<ul>
<li>params&#x3D;{“name”}，表示请求参数必须有name</li>
<li>params&#x3D;{“moeny!100”}，表示请求参数中moeny不能是100</li>
</ul>
</li>
</ul>
<h3 id="2-1-注解扫描"><a href="#2-1-注解扫描" class="headerlink" title="2.1 注解扫描"></a>2.1 注解扫描</h3><blockquote>
<p>第一种</p>
</blockquote>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--Controller组件扫描--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">context:component-scan</span> <span class="attr">base-package</span>=<span class="string">&quot;com.yihen&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">context:include-filter</span> <span class="attr">type</span>=<span class="string">&quot;annotation&quot;</span> <span class="attr">expression</span>=<span class="string">&quot;org.springframework.stereotype.Controller&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">context:component-scan</span>&gt;</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>第二种(推荐)</p>
</blockquote>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--Controller组件扫描, 用来扫描Controller注解--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">context:component-scan</span> <span class="attr">base-package</span>=<span class="string">&quot;com.yihen.controller&quot;</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--mvc注解驱动,用来扫描@RequestMapping--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">mvc:annotation-driven</span>/&gt;</span></span><br></pre></td></tr></table></figure>





<h2 id="3-SpringMVC数据响应"><a href="#3-SpringMVC数据响应" class="headerlink" title="3 SpringMVC数据响应"></a>3 SpringMVC数据响应</h2><h3 id="3-1-页面跳转"><a href="#3-1-页面跳转" class="headerlink" title="3.1 页面跳转"></a>3.1 页面跳转</h3><h4 id="3-1-1-返回字符串形式"><a href="#3-1-1-返回字符串形式" class="headerlink" title="3.1.1 返回字符串形式"></a>3.1.1 返回字符串形式</h4><blockquote>
<p>此种方法会将返回的字符串与视图解析器(如果你配置了的话)的前后缀拼接后进行跳转</p>
</blockquote>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- spring-mvc.xml   配置内部资源视图解析器--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span>   <span class="attr">id</span>=<span class="string">&quot;viewResolver&quot;</span> <span class="attr">class</span>=<span class="string">&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;prefix&quot;</span> <span class="attr">value</span>=<span class="string">&quot;/jsp/&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;suffix&quot;</span> <span class="attr">value</span>=<span class="string">&quot;.jsp&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h4 id="3-1-2-ModelAndView对象返回"><a href="#3-1-2-ModelAndView对象返回" class="headerlink" title="3.1.2 ModelAndView对象返回"></a>3.1.2 ModelAndView对象返回</h4><blockquote>
<p>此方法设置的数据在jsp中可以通过${}表达式取出来</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">   <span class="meta">@RequestMapping(&quot;/user2&quot;)</span></span><br><span class="line">   <span class="comment">// 将ModelAndView写入参数中，spring会给你分配</span></span><br><span class="line">   <span class="function"><span class="keyword">public</span> ModelAndView <span class="title">index2</span><span class="params">(ModelAndView modelAndView)</span> </span>&#123;</span><br><span class="line">       modelAndView.addObject(<span class="string">&quot;name&quot;</span>,<span class="string">&quot;yihen&quot;</span>); <span class="comment">// 设置数据，键值对形式</span></span><br><span class="line">       modelAndView.setViewName(<span class="string">&quot;hello.jsp&quot;</span>); <span class="comment">// 表示要跳转的页面</span></span><br><span class="line">       <span class="keyword">return</span> modelAndView;</span><br><span class="line">   &#125;</span><br><span class="line">   <span class="meta">@RequestMapping(&quot;/user3&quot;)</span></span><br><span class="line"><span class="comment">// model仅用来传递数据</span></span><br><span class="line">   <span class="function"><span class="keyword">public</span> String <span class="title">index3</span><span class="params">(Model model)</span> </span>&#123;</span><br><span class="line">       model.addAttribute(<span class="string">&quot;name&quot;</span>,<span class="string">&quot;HZY&quot;</span>); <span class="comment">// 设置数据，键值对形式</span></span><br><span class="line">       <span class="keyword">return</span> <span class="string">&quot;hello.jsp&quot;</span>;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>





<h3 id="3-2回写数据"><a href="#3-2回写数据" class="headerlink" title="3.2回写数据"></a>3.2回写数据</h3><h4 id="3-2-0-解决返回数据乱码问题"><a href="#3-2-0-解决返回数据乱码问题" class="headerlink" title="3.2.0 解决返回数据乱码问题"></a>3.2.0 解决返回数据乱码问题</h4><blockquote>
<ol>
<li>设置响应体格式</li>
</ol>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(&quot;/user4&quot;)</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">index4</span><span class="params">(HttpServletResponse response)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">     <span class="comment">// 设置响应体格式</span></span><br><span class="line">     response.setContentType(<span class="string">&quot;text/html; charset=UTF-8&quot;</span>);</span><br><span class="line">     response.getWriter().write(<span class="string">&quot;你好&quot;</span>);</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<ol start="2">
<li>配置@RequestMapping属性produces &#x3D; “application&#x2F;json;charset&#x3D;utf-8”v</li>
</ol>
</blockquote>
<blockquote>
<ol start="3">
<li>配置文件(配置在spring-mvc.xml中,要配在注解扫描上面)</li>
</ol>
</blockquote>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">class</span>=<span class="string">&quot;org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter&quot;</span> &gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;messageConverters&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">list</span>&gt;</span></span><br><span class="line">          </span><br><span class="line">            <span class="tag">&lt;<span class="name">bean</span> <span class="attr">class</span>=<span class="string">&quot;org.springframework.http.converter.StringHttpMessageConverter&quot;</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;supportedMediaTypes&quot;</span>&gt;</span></span><br><span class="line">                    <span class="tag">&lt;<span class="name">list</span>&gt;</span></span><br><span class="line">                        <span class="tag">&lt;<span class="name">value</span>&gt;</span>text/plain;charset=utf-8<span class="tag">&lt;/<span class="name">value</span>&gt;</span></span><br><span class="line">                        <span class="tag">&lt;<span class="name">value</span>&gt;</span>text/html;charset=UTF-8<span class="tag">&lt;/<span class="name">value</span>&gt;</span></span><br><span class="line">                    <span class="tag">&lt;/<span class="name">list</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">list</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h4 id="3-2-1-直接返回字符串"><a href="#3-2-1-直接返回字符串" class="headerlink" title="3.2.1 直接返回字符串"></a>3.2.1 直接返回字符串</h4><blockquote>
<ol>
<li>通过SpringMVC框架注入的response对象，使用response.getWriter.print(“xx”)回写数据，此时不需要视图跳转，业务方法返回值为void</li>
</ol>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(&quot;/user4&quot;)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">index4</span><span class="params">(HttpServletResponse response)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">    <span class="comment">// 设置响应体格式</span></span><br><span class="line">    response.setContentType(<span class="string">&quot;text/html; charset=UTF-8&quot;</span>);</span><br><span class="line">    response.getWriter().write(<span class="string">&quot;你好&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<ol start="2">
<li>将需要回写的字符串直接返回，但此时需要通过@ResponseBody注解告知SpringMVC框架，方法返回的字符串不是直接在http响应体中返回</li>
</ol>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@ResponseBody()</span></span><br><span class="line"><span class="comment">// 设置响应utf-8</span></span><br><span class="line"><span class="meta">@RequestMapping(value = &quot;/user5&quot;,produces = &quot;application/json;charset=utf-8&quot;)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">index5</span><span class="params">(HttpServletResponse response)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;你好&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="3-2-2-返回JSON"><a href="#3-2-2-返回JSON" class="headerlink" title="3.2.2  返回JSON"></a>3.2.2  返回JSON</h4><blockquote>
<p> 需要导入json转换工具</p>
</blockquote>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.alibaba<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>fastjson<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.2.62<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">scope</span>&gt;</span>compile<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@ResponseBody</span></span><br><span class="line"><span class="meta">@RequestMapping(&quot;/user6&quot;)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">index6</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    User user = <span class="keyword">new</span> User();</span><br><span class="line">    user.setUsername(<span class="string">&quot;懿痕&quot;</span>);</span><br><span class="line">    user.setAge(<span class="number">19</span>);</span><br><span class="line">    <span class="comment">// 使用json的转换工具 将对象转换成json格式的字符串</span></span><br><span class="line">    String s = JSON.toJSONString(user);</span><br><span class="line">    <span class="keyword">return</span> s;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>





<h4 id="3-2-3-返回对象或集合"><a href="#3-2-3-返回对象或集合" class="headerlink" title="3.2.3 返回对象或集合"></a>3.2.3 返回对象或集合</h4><blockquote>
<p>转换成json格式回写即可</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@ResponseBody</span></span><br><span class="line"><span class="meta">@RequestMapping(&quot;/user7&quot;)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">index7</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    User user = <span class="keyword">new</span> User();</span><br><span class="line">    User user1 = <span class="keyword">new</span> User();</span><br><span class="line">    User user2 = <span class="keyword">new</span> User();</span><br><span class="line"></span><br><span class="line">    user.setUsername(<span class="string">&quot;懿痕&quot;</span>);</span><br><span class="line">    user.setAge(<span class="number">19</span>);</span><br><span class="line">    user1.setUsername(<span class="string">&quot;懿痕1&quot;</span>);</span><br><span class="line">    user1.setAge(<span class="number">191</span>);</span><br><span class="line">    user2.setUsername(<span class="string">&quot;懿痕2&quot;</span>);</span><br><span class="line">    user2.setAge(<span class="number">192</span>);</span><br><span class="line">    List&lt;User&gt; list = <span class="keyword">new</span> ArrayList();</span><br><span class="line">    list.add(user);</span><br><span class="line">    list.add(user1);</span><br><span class="line">    list.add(user2);</span><br><span class="line">    <span class="comment">// 使用json的转换工具 将对象转换成json格式的字符串</span></span><br><span class="line">    String s = JSON.toJSONString(list);</span><br><span class="line">    <span class="keyword">return</span> s;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="4-SpringMVC获取请求数据"><a href="#4-SpringMVC获取请求数据" class="headerlink" title="4. SpringMVC获取请求数据"></a>4. SpringMVC获取请求数据</h2><blockquote>
<p>开发资源访问(配置spring-mvc.xml)</p>
</blockquote>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--    开发资源的访问--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">mvc:resources</span> <span class="attr">mapping</span>=<span class="string">&quot;/js/**&quot;</span> <span class="attr">location</span>=<span class="string">&quot;/js/&quot;</span>/&gt;</span></span><br></pre></td></tr></table></figure>

<p>或</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">mvc:default-servlet-handler</span>/&gt;</span></span><br></pre></td></tr></table></figure>



<h3 id="4-1-获得基本类型数据"><a href="#4-1-获得基本类型数据" class="headerlink" title="4.1 获得基本类型数据"></a>4.1 获得基本类型数据</h3><blockquote>
<p>Controller中的业务方法的参数名称要与请求参数的name一致，参数值会自动映射匹配。</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@ResponseBody</span></span><br><span class="line"><span class="meta">@RequestMapping(&quot;/user8&quot;)</span></span><br><span class="line"><span class="comment">// 但请求中的参数与形参名对应上时会自动匹配值进去</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">index8</span><span class="params">(String username,<span class="keyword">int</span> age)</span> </span>&#123;</span><br><span class="line">    System.out.println(username);</span><br><span class="line">    System.out.println(age);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="4-2-获取POJO类型参数"><a href="#4-2-获取POJO类型参数" class="headerlink" title="4.2 获取POJO类型参数"></a>4.2 获取POJO类型参数</h3><blockquote>
<p>Controlle中的业务方法的POJO参数的属性名与请求参数的name一致，参数值会自动映射匹配</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@ResponseBody</span></span><br><span class="line"><span class="meta">@RequestMapping(&quot;/user9&quot;)</span></span><br><span class="line"><span class="comment">// 但请求中的参数与形参名对应上时会自动匹配值进去</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">index9</span><span class="params">(User user)</span> </span>&#123;</span><br><span class="line">    System.out.println(user.toString());  <span class="comment">// User(username=yihen, age=18)</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="4-3-获取数组类型参数"><a href="#4-3-获取数组类型参数" class="headerlink" title="4.3 获取数组类型参数"></a>4.3 获取数组类型参数</h3><blockquote>
<p>Controller中的业务方法数组名称与请求参数的name一致，参数会自动映射匹配</p>
<p>请求：<a target="_blank" rel="noopener" href="http://localhost:8080/spring_mvc/user/quick13?strs=aaa&amp;strs=bbb&amp;strs=ccc">http://localhost:8080/spring_mvc/user/quick13?strs=aaa&amp;strs=bbb&amp;strs=ccc</a></p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@ResponseBody</span></span><br><span class="line"><span class="meta">@RequestMapping(value = &quot;/user10&quot;)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">index10</span><span class="params">(String[] strs)</span> </span>&#123;</span><br><span class="line">    System.out.println(Arrays.asList(strs)); <span class="comment">// [aaa, bbb, ccc]</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="4-4-获取集合类型参数"><a href="#4-4-获取集合类型参数" class="headerlink" title="4.4 获取集合类型参数"></a>4.4 获取集合类型参数</h3><blockquote>
<p>当使用Ajax提交时，可以指定contentType为json形式，那么在方法参数位置使用@RequestBody可以直接接收集合数据而无需使用POJO进行包装</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">    <span class="meta">@ResponseBody</span></span><br><span class="line">    <span class="meta">@RequestMapping(value = &quot;/user11&quot;)</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">index11</span><span class="params">(<span class="meta">@RequestBody</span> List&lt;User&gt; users)</span> </span>&#123;</span><br><span class="line">        System.out.println(users);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="4-5-文件上传"><a href="#4-5-文件上传" class="headerlink" title="4.5 文件上传"></a>4.5 文件上传</h3><blockquote>
<p>文件上传三要素:</p>
<ul>
<li>表单项: type&#x3D;”file”</li>
<li>表单提交方式: post</li>
<li>表单的enctype属性是多部分表单形式，即enctype&#x3D;”multipart&#x2F;form-data”</li>
</ul>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">form</span> <span class="attr">action</span>=<span class="string">&quot;$&#123;pageContext.request.contextPath&#125;/user/quick22&quot;</span> <span class="attr">method</span>=<span class="string">&quot;post&quot;</span> <span class="attr">enctype</span>=<span class="string">&quot;multipart/form-data&quot;</span>&gt;</span></span><br><span class="line">    名称<span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">name</span>=<span class="string">&quot;username&quot;</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    文件<span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;file&quot;</span> <span class="attr">name</span>=<span class="string">&quot;upload&quot;</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">&quot;submit&quot;</span>&gt;</span>提交 <span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">form</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h4 id="4-5-1-单文件上传"><a href="#4-5-1-单文件上传" class="headerlink" title="4.5.1 单文件上传"></a>4.5.1 单文件上传</h4><blockquote>
<ol>
<li>导入fileupload和io坐标</li>
</ol>
</blockquote>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>commons-fileupload<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>commons-fileupload<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.3.1<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>commons-io<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>commons-io<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.3<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>



<blockquote>
<ol start="2">
<li>配置上传解析器</li>
</ol>
</blockquote>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--    配置文件上传解析器--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">class</span>=<span class="string">&quot;org.springframework.web.multipart.commons.CommonsMultipartResolver&quot;</span> <span class="attr">id</span>=<span class="string">&quot;multipartResolver&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;defaultEncoding&quot;</span> <span class="attr">value</span>=<span class="string">&quot;UTF-8&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;maxUploadSize&quot;</span> <span class="attr">value</span>=<span class="string">&quot;500000&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br></pre></td></tr></table></figure>



<blockquote>
<ol start="3">
<li>编写上传代码</li>
</ol>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@ResponseBody</span></span><br><span class="line"><span class="meta">@RequestMapping(value = &quot;/user13&quot;)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">index13</span><span class="params">(String username, MultipartFile upload)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">    System.out.println(username);</span><br><span class="line">    <span class="comment">// 获取文件上传名称</span></span><br><span class="line">    String name = upload.getOriginalFilename();</span><br><span class="line">    <span class="comment">// 转存</span></span><br><span class="line">    upload.transferTo(<span class="keyword">new</span> File(<span class="string">&quot;D:\\upload\\&quot;</span>+name));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>





<h4 id="4-5-2-多文件上传"><a href="#4-5-2-多文件上传" class="headerlink" title="4.5.2 多文件上传"></a>4.5.2 多文件上传</h4><blockquote>
<ol>
<li>第一种 添加参数</li>
</ol>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@ResponseBody</span></span><br><span class="line"><span class="meta">@RequestMapping(value = &quot;/quick22&quot;)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span>  <span class="title">save22</span><span class="params">(String username, MultipartFile upload,MultipartFile upload2)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">    System.out.println(username);</span><br><span class="line">    System.out.println(upload);</span><br><span class="line">    <span class="comment">// 获取文件上传的名称</span></span><br><span class="line">    String originalFilename = upload.getOriginalFilename();</span><br><span class="line">    String originalFilename1 = upload2.getOriginalFilename();</span><br><span class="line">    upload.transferTo(<span class="keyword">new</span> File(<span class="string">&quot;D:\\upload\\&quot;</span>+originalFilename));</span><br><span class="line">    upload2.transferTo(<span class="keyword">new</span> File(<span class="string">&quot;D:\\upload\\&quot;</span>+originalFilename1));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<ol start="2">
<li>第二种: 表单文件处名字统一</li>
</ol>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@ResponseBody</span></span><br><span class="line"><span class="meta">@RequestMapping(value = &quot;/quick23&quot;)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span>  <span class="title">save23</span><span class="params">(String username, MultipartFile[] upload)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">    <span class="keyword">for</span>(MultipartFile item: upload)&#123;</span><br><span class="line">        String originalFilename = item.getOriginalFilename();</span><br><span class="line">        System.out.println(originalFilename);</span><br><span class="line">        item.transferTo(<span class="keyword">new</span> File(<span class="string">&quot;D:\\upload\\&quot;</span>+originalFilename));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="5-SpringMVC拦截器"><a href="#5-SpringMVC拦截器" class="headerlink" title="5. SpringMVC拦截器"></a>5. SpringMVC拦截器</h2><h3 id="5-1-拦截器简介"><a href="#5-1-拦截器简介" class="headerlink" title="5.1 拦截器简介"></a>5.1 拦截器简介</h3><blockquote>
<ol>
<li>拦截器作用</li>
</ol>
</blockquote>
<p>SpringMVC的拦截器类似于Servlet开发中的Filter，用于对处理器进行预处理和后处理.</p>
<p>将拦截器按一定的顺序连接成一条链，这条链称为&#x3D;&#x3D;拦截器链（interceptor chain）&#x3D;&#x3D;。在访问被拦截的方法或字段时，拦截器中的拦截器就会按其之前定义的顺序被调用。拦截器也是AOP思想的具体体现.</p>
<blockquote>
<ol start="2">
<li>拦截器与过滤器的区别</li>
</ol>
</blockquote>
<p><img src="https://s2.loli.net/2022/02/01/f6wBlZV42YSna3g.png" alt="image-20220201115922792"></p>
<h3 id="5-2-快速入门"><a href="#5-2-快速入门" class="headerlink" title="5.2 快速入门"></a>5.2 快速入门</h3><blockquote>
<ol>
<li>创建拦截器累类,实现HandlerInterceptor接口</li>
</ol>
<p>&#x3D;&#x3D;preHandle()返回true代表放行&#x3D;&#x3D;</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Interceptor</span> <span class="keyword">implements</span> <span class="title">HandlerInterceptor</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">     <span class="comment">// 在目标方法执行之前执行</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">preHandle</span><span class="params">(HttpServletRequest request, HttpServletResponse response, Object handler)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;拦截前...&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;<span class="comment">// true表示放行</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="comment">// 在目标方法执行之后，视图返回之前执行</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">postHandle</span><span class="params">(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;postHandle...&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="comment">// 在整个流程都执行完毕后执行</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">afterCompletion</span><span class="params">(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;执行完收尾...&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<blockquote>
<ol start="2">
<li>配置拦截器</li>
</ol>
</blockquote>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--    配置拦截器--&gt;</span>   </span><br><span class="line"><span class="tag">&lt;<span class="name">mvc:interceptors</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">mvc:interceptor</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!--代表对哪些资源进行拦截操作   --&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">mvc:mapping</span> <span class="attr">path</span>=<span class="string">&quot;/**&quot;</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">bean</span> <span class="attr">class</span>=<span class="string">&quot;com.yihen.interceptor.Interceptor&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">mvc:interceptor</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">mvc:interceptors</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h3 id="5-3-拦截器方法说明"><a href="#5-3-拦截器方法说明" class="headerlink" title="5.3 拦截器方法说明"></a>5.3 拦截器方法说明</h3><p><img src="https://s2.loli.net/2022/02/01/pgscxJmXiWV9r5k.png" alt="image-20220201131953985"></p>

            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls" data-autoplay="false">
                <source type="audio/mpeg" src="">
            </audio>
            
                <ul id="audio-list" style="display:none">
                    
                        
                            <li title="0" data-url="http://link.hhtjim.com/163/425570952.mp3"></li>
                        
                    
                        
                            <li title="1" data-url="http://link.hhtjim.com/163/425570952.mp3"></li>
                        
                    
                </ul>
            
        </div>
        
    <div id="gitalk-container" class="comment link"
		data-enable="false"
        data-ae="false"
        data-ci=""
        data-cs=""
        data-r=""
        data-o=""
        data-a=""
        data-d="false"
    >查看评论</div>


    </div>
    
</div>


    </div>
</div>
</body>


<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="/yi-mark-gitee-io/js/plugin.js"></script>
<script src="/yi-mark-gitee-io/js/typed.js"></script>
<script src="/yi-mark-gitee-io/js/diaspora.js"></script>


<link rel="stylesheet" href="/yi-mark-gitee-io/photoswipe/photoswipe.css">
<link rel="stylesheet" href="/yi-mark-gitee-io/photoswipe/default-skin/default-skin.css">


<script src="/yi-mark-gitee-io/photoswipe/photoswipe.min.js"></script>
<script src="/yi-mark-gitee-io/photoswipe/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>






</html>
